/*
    * @module:views/budget/investBudget/modulationList
    * @author:silas
    * @date:2021-02-28
    * 预算下达
*/
<template>
    <div class="budgetRelease_container">
        <el-form :inline="true" :model="form" label-width="60px" class="m10">
            <el-form-item label="区域">
                <dx-cascader :area_id.sync="form.areaId" :maxLevel="2" />
            </el-form-item>
            <el-form-item label="年度">
                <el-date-picker
                    size="mini"
                    v-model="form.year"
                    value-format="yyyy"
                    type="year"
                    placeholder="选择年"
                ></el-date-picker>
            </el-form-item>
            <el-form-item label="投资专项" label-width="80">
                <el-select v-model="form.specialId" size="mini" clearable>
                    <el-option
                        v-for="(it, i) in idList"
                        :key="i"
                        :label="it.specialName"
                        :value="it.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="状态">
                <el-select v-model="form.status" size="mini" clearable>
                    <el-option
                        v-for="(it, i) in statusList"
                        :key="i"
                        :label="it.label"
                        :value="it.value"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button size="mini" type="primary" @click="handle_search()"
                    >查询</el-button
                >
            </el-form-item>
        </el-form>
        <div>
            <div class="modulation">
                <div class="tableBox">
                    <el-table
                        height="73vh"
                        ref="table"
                        size="mini"
                        :header-cell-style="{ background: '#eef1f6' }"
                        :data="tableData"
                        v-loading="tableLoading"
                        border
                        style="width: 100%"
                    >
                        <el-table-column
                            label="预算年度"
                            prop="testYear"
                        ></el-table-column>
                        <el-table-column
                            min-width="120"
                            label="分公司"
                            prop="areaName"
                        ></el-table-column>
                        <el-table-column
                            label="申请人"
                            prop="updateUserName"
                        ></el-table-column>
                        <el-table-column
                            label="下达专项"
                            prop="specialName"
                        ></el-table-column>
                        <el-table-column
                            min-width="140"
                            show-overflow-tooltip
                            label="调整说明"
                            prop="updateExplain"
                        ></el-table-column>
                        <el-table-column
                            width="120"
                            label="申请时间"
                            prop="applyTimeStr"
                        ></el-table-column>
                        <el-table-column
                            width="120"
                            label="审批时间"
                            prop="auditTimeStr"
                        ></el-table-column>
                        <el-table-column
                            min-width="140"
                            show-overflow-tooltip
                            label="审批说明"
                            prop="auditExplain"
                        ></el-table-column>
                        <el-table-column
                            label="状态"
                            prop="statuText"
                        ></el-table-column>
                        <el-table-column label="操作" min-width="160">
                            <template slot-scope="scope">
                                <el-button
                                    type="text"
                                    :disabled="
                                        lvl == 2 || scope.row.status != '0'
                                    "
                                    @click="handle_edit(scope.row)"
                                    >编辑</el-button
                                >
                                <el-button
                                    type="text"
                                    @click="handle_detail(scope.row)"
                                    >详情</el-button
                                >
                                <el-button
                                    type="text"
                                    :disabled="
                                        lvl != 2 || scope.row.status != '1'
                                    "
                                    @click="handle_check(scope.row)"
                                    >审核</el-button
                                >
                                <!-- <el-button
                                    type="text"
                                    :disabled="
                                        lvl == 2 || scope.row.status == '2'
                                    "
                                    @click="handle_del(scope.row, 0)"
                                    >删除</el-button
                                >
                                <el-button
                                    type="text"
                                    :disabled="
                                        lvl == 3 || scope.row.status != '2'
                                    "
                                    @click="handle_del(scope.row, 1)"
                                    >作废</el-button
                                > -->
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { $budgetAPI } from "@/api/";
import { getSessionStorage } from "@/common/js/storage";
import prefixAPI from "@/api/prefixAPI";
import { getCookie } from "@/common/js/cookie";
export default {
    data() {
        return {
            form: {
                areaId: "",
                year: this.moment().format("YYYY"),
                specialId: "",
                status: "",
            },
            yearList: [],
            tableData: [],
            accountInfo: null,
            tableLoading: false,
            fileList: [],
            param: "",
            lvl: false,
            desc: "",
            statusList: [],
            idList: [],
            type: "",
        };
    },
    watch: {
        "form.year"(newValue, oldValue) {
            var that = this;
            console.log(newValue, oldValue)
            $budgetAPI.getIDList(
                {
                    acctYear: that.form.year,
                },
                (res) => {
                    that.idList = [{
                        id: "",
                        specialName: "全部",
                    }, ...res.content];
                }
            );
        }
    },
    mounted() {
        var that = this;

        if(that.$route.query && that.$route.query.type == 'modulationList') {
             that.init()
        }
    },
    methods: {
        init() {
            var that = this,
                scopedSlots;
            that.accountInfo = JSON.parse(getSessionStorage("accountInfo"));
            that.form.areaId = that.accountInfo.areaId;
            that.lvl = that.accountInfo.areaLvl;
            that.statusList =
                that.lvl == 2
                    ? [
                          {
                              label: "全部",
                              value: "",
                          },
                          {
                              label: "待审核",
                              value: 1,
                          },
                          {
                              label: "审核通过",
                              value: 2,
                          },
                          {
                              label: "审核未通过",
                              value: 3,
                          },
                      ]
                    : [
                          {
                              label: "全部",
                              value: "",
                          },
                          {
                              label: "待提交",
                              value: 0,
                          },
                          {
                              label: "待审核",
                              value: 1,
                          },
                          {
                              label: "审核通过",
                              value: 2,
                          },
                          {
                              label: "审核未通过",
                              value: 3,
                          },
                          {
                              label: "已作废",
                              value: 4,
                          },
                      ];
            that.getInitData();
        },
        // 初始化
        async getInitData() {
            let that = this,
                list;
            // that.yearList = await that.getReleaseMonths();
            list = await that.getIdList();
            that.idList = [{
                    id: "",
                    specialName: "全部",
                }, ...list];
            that.getList();
        },
        getIdList() {
            var that = this;

            return new Promise((resolve) => {
                $budgetAPI.getIDList(
                    {
                        acctYear: that.form.year,
                    },
                    (res) => {
                        resolve(res.content);
                    }
                );
            });
        },
        getList() {
            var that = this,
                status = [
                    "待提交",
                    "待审核",
                    "审核通过",
                    "审核未通过",
                    "已作废",
                ];

            $budgetAPI.getJiLuRecordList(
                {
                    areaId: that.form.areaId,
                    acctYear: that.form.year,
                    specialId: that.form.specialId,
                    status: that.form.status,
                    token: getCookie("token"),
                },
                (res) => {
                    res.content.data.map((it) => {
                        it.statuText = status[it.status];
                    });
                    that.tableData = res.content.data;
                }
            );
        },
        handle_check(row) {
            this.$router.push({
                path: "/budget/modulation",
                query: { id: row.id, type: "check" },
            });
        },
        handle_edit(row) {
            this.$router.push({
                path: "/budget/modulation",
                query: { id: row.releaseId, type: "change" },
            });
        },
        handle_del(row, type) {
            var that = this;

            that.$confirm(!type ? "是否删除?" : "是否作废", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                $budgetAPI.getJiLuRecordDelete(
                    {
                        recordId: row.id,
                        status: type,
                        token: getCookie("token"),
                    },
                    (res) => {
                        if (res.code == 0) {
                            this.$message({
                                type: "success",
                                message: "删除成功!",
                            });
                        } else {
                            this.$message({
                                type: "error",
                                message: res.message,
                            });
                        }
                        that.getList();
                    }
                );
            });
        },
        handle_save(type) {
            var that = this,
                obj = {};

            that.tableData.map((it) => {
                obj[it.indexCode] = it[it.indexCode];
            });

            $budgetAPI.getReleaseModify(
                {
                    token: getCookie("token"),
                    modifyReleaseStr: JSON.stringify(obj),
                    isSubmit: type,
                    releaseId: that.id,
                    recordId: "",
                },
                (res) => {
                    console.log(res);
                }
            );
        },
        handle_detail(row) {
            var that = this;

            that.$router.push({
                path: "/budget/modulation",
                query: { id: row.id, type: "detail" },
            });
        },
        handle_search() {
            var that = this;

            that.getList();
        },
    },
};
</script>
<style lang="scss" scoped>
.g-footer {
    display: flex;
}
</style>